﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WAD.Model;
using System.Threading;
using WAD.DAL;

namespace WAD.WEB.AppCode
{
    public class ClientClick
    {

        /// <summary>
        /// 点击
        /// </summary>
        /// <param name="sharedId">分享id</param>
        /// <param name="type">点击类型   1表示文章阅读量   2表示广告点击量</param>
        public static void Click(int sharedId, int type, int advert_id,int clickNumber)
        {
            HttpRequest request = HttpContext.Current.Request;

            String ip = request.UserHostAddress;//获取客户机ip
            String userAgent = request.UserAgent;

            try
            {
                //通过分享信息获取分享id
                MSharedClick m = new MSharedClick();
                m.ip = ip;
                m.shared_id = sharedId;
                m.user_agent = userAgent;

                String _userAgent = userAgent.ToLower();


                //获取用户的操作系统
                if (_userAgent.IndexOf("android") != -1)
                {
                    m.os = "Android";
                }
                else if (_userAgent.IndexOf("iphone") != -1)
                {
                    m.os = "IOS";
                }               
                else if (_userAgent.IndexOf("windows nt") != -1)
                {
                    m.os = "Windows";
                }
                else if (_userAgent.IndexOf("ipad") != -1)
                {
                    m.os = "iPad";
                }
                else if (_userAgent.IndexOf("windows phone") != -1)
                {
                    m.os = "Windows Phone";
                }
                else
                {
                    DaoLogcat.Add("click", "未能识别到系统  UserAgent = " + userAgent);
                }


                //未能识别到浏览器  UserAgent = Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13D15wjx

                //浏览器名称
                if (_userAgent.IndexOf("micromessenger") != -1)
                {
                    m.browser = "微信";
                }
                else if (_userAgent.IndexOf("chrome") != -1)
                {
                    m.browser = "谷歌";
                }
                else if (_userAgent.IndexOf("mqqbrowser") != -1)
                {
                    m.browser = "QQ浏览器";
                }
                else if (_userAgent.IndexOf("ucbrowser") != -1)
                {
                    m.browser = "UC浏览器";
                }
                else if (_userAgent.IndexOf("baidu") != -1)
                {
                    m.browser = "百度浏览器";
                }
                else if (_userAgent.IndexOf("msie") != -1)
                {
                    m.browser = "IE浏览器";
                }
                else if (_userAgent.IndexOf("safari") != -1 && _userAgent.IndexOf("mac os x") != -1)
                {
                    m.browser = "Safari";
                }
                else if (_userAgent.IndexOf("wjx") > -1)
                {
                    m.browser = "微街秀APP";   //2016-02-24 add wanghui
                }
                else
                {
                    DaoLogcat.Add("click", "未能识别到浏览器  UserAgent = " + userAgent);
                }

                DaoStatistics.Click(m, type, advert_id, clickNumber);
            }
            catch(Exception e)
            {
                DaoLogcat.Add("error", e.Message);
            }
        }
    }
}